<?php 
	$input = (isset($_POST["input"])) ? htmlspecialchars($_POST["input"], ENT_QUOTES) : "";
	$align = (isset($_POST["align"])) ? htmlspecialchars($_POST["align"], ENT_QUOTES) : "local";
	$sum = (isset($_POST["sum"])) ? htmlspecialchars($_POST["sum"], ENT_QUOTES) : 50;
	$doCompare = (isset($_POST["doCompare"])) ? htmlspecialchars($_POST["doCompare"], ENT_QUOTES) : false;
	$inputString = (isset($_POST["inputString"])) ? strtoupper(htmlspecialchars($_POST["inputString"], ENT_QUOTES)) : "";
	
	if ($input == 'file' && $_FILES['inputFile']['error'] == 0) {
		$inputs = readFileToArray($_FILES['inputFile']['tmp_name']);
		$inputString = trim($inputs[0]);
	}
?><?php echo'<?xml version="1.0" encoding="utf-8"?>'; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<title>bioinformatika – podobnost dna sekvencí</title>
	<link rel="stylesheet" href="css/style.css" type="text/css" media="screen" />
	<script type="text/javascript" src="js/mootools-123-core.js"></script>
	<script type="text/javascript" src="js/toggler.js"></script>
</head>
<body>
	<h1>bioinformatika – podobnost dna sekvencí <small>Semestrální projekt VMW, David Slačálek, Matus Szepe</small></h1>
	<form action="" method="post" enctype="multipart/form-data">
		<p>
			<span class="label">vstup: </span>
			
			<input type="radio" class="indent" id="input1" name="input" value="string" <?php if ($input=="string") echo 'checked="checked"'; ?> />
			<label for="input1"> řetězec</label>
			<input type="text" id="inputString" name="inputString" value="<?php echo $inputString; ?>" />
			<br />
			<input type="radio" class="indent" id="input2" name="input" value="file" <?php if ($input=="file") echo 'checked="checked"'; ?>  />
			<label for="input2"> soubor</label>				
			<input type="file" id="inputFile" name="inputFile" />
		</p>
		
		<p>
			<span class="label">zarovnání: </span>
			
			<input type="radio" class="indent" id="align1" name="align" value="local" <?php if ($align=="local") echo 'checked="checked"'; ?> />
			<label for="align1">lokální</label>
			<br />
			<input type="radio" class="indent" id="align2" name="align" value="global" <?php if ($align=="global") echo 'checked="checked"'; ?> />
			<label for="align2">globální</label>
			
		</p>
		<p>
			<span class="label"><label for="sum">počet DNA: </label></span>
			<input type="text"  class="indent" id="sum" name="sum" value="<?php if ($sum!="") echo $sum; ?>" /> vzorků (max. 587).
			
		</p>
		<p>
			<input type="submit" value="porovnat"/>
			<input type="hidden" name="doCompare" value="true" />
		</p>
	</form>
	<!-- <p>
		<span class="label">řetězec:</span><br />
		<?php echo $inputString; ?>
	</p> -->
	<?php 
		if ($doCompare) {
			
			if (preg_match("/[^ACGT]/",$inputString) || $inputString == "") {
				?><p class="error"><strong>chyba!</strong> řetězec smí obsahovat pouze znaky <strong>A</strong>, <strong>C</strong>, <strong>G</strong> a <strong>T</strong></p><?php
			}
			elseif ($input == 'file' && $_FILES['inputFile']['error'] != 0) {
				?><p class="error"><strong>chyba!</strong>  nahrávanému souboru se přihodilo něco nemilého (kód <?php echo $_FILES['inputFile']['error']; ?>)<?php
			}
			elseif ($sum > 587 || $sum < 0) {
				?><p class="error"><strong>chyba!</strong> počet vzorků musí být z intervalu &lt;1, 587&gt;<?php
			}
			else {
				require_once('Classes/DnaDb.php');
				$dnaHandle = new DnaDb();
				$data = $dnaHandle->getAllDna();
				$compares = array();
				//echo "DATA COUNT ".count($data);
				switch ($align) {
					case "local":
						require_once('Classes/LocalAlignator.php');
						$g = new LocalAlignator('', '');
						break;
					case "global":
						require_once('Classes/GlobalAlignator.php');
						$g = new GlobalAlignator('', '');
						break;
					default:
						break;
				
				}
				
				/* TIMETRACKING STARTS */
					$mtime = microtime();
					$mtime = explode(" ", $mtime);
					$mtime = $mtime[1] + $mtime[0];
					$starttime = $mtime;
				/* TIMETRACKING ENDS */
				
				//comparing strings
				for ($i = 0; $i < $sum; $i++) {
					$string2 = $data[$i];
					$g->run($inputString, $string2['dnaString']);
					//echo $string2['dnaString'].': '.$g->getPathScore().'<br />';
					$pathScore = $g->getPathScore();
					/* ?><pre><?php
					print_r($pathScore);
					?></pre><?php  */
					$strings = $g->getComparedStrings();
					$compares[$i]['dnaKey'] = $string2['dnaKey'];
					$compares[$i]['pathScore'] = $pathScore;
					$compares[$i]['dnaString'] = $string2['dnaString'];
					$compares[$i]['comparsion'] = $inputString."\n".$string2['dnaString']."\n\n".$strings['string1']."\n".$strings['compare']."\n".$strings['string2'];
				}
				/* TIMETRACKING RESULTS */
					$mtime = microtime();
					$mtime = explode(" ", $mtime);
					$mtime = $mtime[1] + $mtime[0];
					$endtime = $mtime;
					$totaltime = ($endtime - $starttime);
				/* TIMETRACKING RESULTS ENDS */
				
				//sorting results
				usort($compares, function($a, $b) {
				    return $a['pathScore'] - $b['pathScore'];
				});
				$comparesDesc = array_reverse($compares);
				
				//presenting results
				?><div id="resultList"><?php
				$i=0;
				foreach($comparesDesc as $v){ ?>
					<div class="result">
						<p class="info">
							<strong><?php echo ++$i; ?></strong>.
							skóre: <strong><?php echo $v['pathScore']; ?>,</strong>
							id: <?php echo $v['dnaKey']; ?>,
							řetězec: <?php echo $v['dnaString']; ?>
							<span class="fade">&nbsp;</span>
							<span class="expand">+</span>
						</p>
						<div class="preContainer"><pre><?php echo $v['comparsion']; ?></pre></div>
						
					</div>
				<?php }
				?></div>
				<p id="time">
					<span class="label">čas: </span><br />
					<?php echo $totaltime; ?>
				</p><?php
			}
		}
	?>
</body>
</html>
<?php 
	function readFileToArray($path){
		$lines = file($path);
		if (!$lines) return false;
		return $lines;
	}
?>
